package com.example.dell.fastshopping.user.task;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.utils.DateUtils;
import com.example.dell.fastshopping.utils.DensityUtil;
import com.example.dell.fastshopping.utils.GlideUtils;
import com.example.dell.fastshopping.utils.ToastUtils;
import com.example.dell.fastshopping.utils.ZxingUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${YSH} on 2019/5/9.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class JoinPageActivity extends AppCompatActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tip)
    TextView tip;
    @BindView(R.id.invite_code)
    EditText inviteCode;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.qrcode)
    ImageView qrcode;
    @BindView(R.id.hint1)
    TextView hint1;
    @BindView(R.id.hint2)
    TextView hint2;
    @BindView(R.id.hint3)
    TextView hint3;
    private JoinViewModel viewmodel;
    private int type;
    private CodeInfo mCodeInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_joinpage);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        title.setText("激活验证");
        viewmodel = ViewModelProviders.of(this).get(JoinViewModel.class);
        type = getIntent().getIntExtra("type", -2);
        //1:qq群 2:微信群 3:公众号
        if (type == 2) {
            tip.setText("请输入微信群激活码");
            hint1.setText("扫码加入微信群");
            hint2.setText("加入微信群-通过群公告获得");
            hint3.setText("客服号：A454565");
            hint3.setVisibility(View.VISIBLE);
        } else if (1 == type) {
            tip.setText("请输入QQ群激活码");
            hint1.setText("扫码加入QQ群");
            hint2.setText("加入QQ群(8754444)-通过群公告获得");
            hint3.setVisibility(View.GONE);
        } else {
            tip.setText("请输入微信公众号激活码");
            hint1.setText("扫码关注微信公众号");
            hint2.setText("关注公众号-回复“激活码”-获得激活码");
            hint3.setVisibility(View.GONE);
        }

        viewmodel.getQrCode(type).observe(this, new Observer<CodeInfo>() {
            @Override
            public void onChanged(@Nullable CodeInfo codeinfo) {
                //二维码
                mCodeInfo = codeinfo;
                getQrCode(mCodeInfo.getGroupUrl());

                if (type == 2) {
                    hint3.setText("客服号：" + mCodeInfo.getGroupNum());
                } else if (1 == type) {
                    hint2.setText("加入QQ群(" + mCodeInfo.getGroupNum() + ")-通过群公告获得");
                }
            }
        });
    }

    private void getQrCode(String address) {
        //判断URL合法性
        if (address == null || "".equals(address) || address.length() < 1) {
            ToastUtils.showShort("二维码为空");
            return;
        }
        int px = DensityUtil.dp2px(this, 184);
        Bitmap bitmap = ZxingUtils.createBitmap(address, px, px);
        Glide.with(this).load(bitmap)
                .error(R.mipmap.default_user_icon) //异常时候显示的图片
                .placeholder(R.mipmap.default_user_icon) //加载成功前显示的图片...
                .into(qrcode);

    }

    @OnClick({R.id.back, R.id.confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.confirm:
                jihuo();
                break;
        }
    }

    private void jihuo() {
        String code = inviteCode.getText().toString().trim();
        if (code.isEmpty()) {
            ToastUtils.showShort("请填写激活码！");
            return;
        }
        viewmodel.checkJihuo(type, code).observe(this, new Observer<BaseResponse>() {
            @Override
            public void onChanged(@Nullable BaseResponse response) {
                if (response.isSuccess()) {
                    ToastUtils.showShort("成功");
                    finish();
                } else {
                    ToastUtils.showShort("失败");
                }
            }
        });
    }
}
